﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YJKRevitDimensionText.VerticalAutoDim.ProgressBar
{
	public partial class ProgressBarForm : Form, IDualStatusProgressBarProcess
	{
		public bool IsStop { get; set; }

		public ProgressBarForm()
		{
			this.InitializeComponent();
            //base.Icon = Resources.YArchitech;
			this.label1.Visible = false;
			this.labelStatus.Visible = false;
			this.progressBarSub.Visible = false;
			this.labelTime.Visible = false;
			this.mapPrimaryStatusData = new Dictionary<int, object>();
			this.IsStop = false;
			this.Stopwatch.Start();
			this.Time.Start();
			this.Time.Tick += this.Time_Tick;
		}

		private void Time_Tick(object sender, EventArgs e)
		{
			this.TimSpan = this.Stopwatch.Elapsed;
			this.labelTime.Text = string.Format("总耗时:{0}分:{1}秒", this.TimSpan.Minutes, this.TimSpan.Seconds);
			Application.DoEvents();
		}

		public bool AddSubStatusData(int iStatusCode, object objStatusData)
		{
			try
			{
				if (!this.mapPrimaryStatusData.ContainsKey(iStatusCode))
				{
					this.mapPrimaryStatusData.Add(iStatusCode, objStatusData);
					this.progressBarPrimary.Minimum = 0;
					this.progressBarPrimary.Maximum = this.mapPrimaryStatusData.Count;
					return true;
				}
			}
			catch
			{
			}
			return false;
		}

		public void UpdatePrimaryStatus(int iStatusCode)
		{
			if (iStatusCode < this.mapPrimaryStatusData.Count)
			{
				this.progressBarPrimary.Value = iStatusCode + 1;
				PrimaryStatusData primaryStatusData = this.mapPrimaryStatusData[iStatusCode] as PrimaryStatusData;
				this.labelStatus.Text = primaryStatusData.StatusValue;
				this.labelMain.Text = string.Format("{0}/{1}", iStatusCode + 1, this.mapPrimaryStatusData.Count);
				Application.DoEvents();
			}
		}

		public void SetSubStatusMaxVal(int iMax)
		{
			this.SubMaxVal = iMax;
			double num = (double)this.progressBarSub.Maximum - (double)this.progressBarSub.Minimum;
			this.SubStepVal = (double)iMax / num;
		}

		public void UpdateSubStatus(int iSubStatusPercentage)
		{
			if (this.IsStop)
			{
				throw new ProgressUserTerminalException();
			}
			if (iSubStatusPercentage < this.SubMaxVal)
			{
				this.progressBarSub.Value = (int)((double)(iSubStatusPercentage + 1) / this.SubStepVal);
				Application.DoEvents();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);
			if (!base.ControlBox)
			{
				return;
			}
			if (YJKMessageBox.Show("是否取消当前操作?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
			{
				this.IsStop = false;
				e.Cancel = true;
				return;
			}
			this.IsStop = true;
		}

		private readonly Dictionary<int, object> mapPrimaryStatusData;

		private readonly Timer Time = new Timer();

		private readonly Stopwatch Stopwatch = new Stopwatch();

		private TimeSpan TimSpan;

		private double SubStepVal;

		private int SubMaxVal;
	}
}
